package com.neteast.androidclient.newscenter.util;


import com.neteast.androidclient.newscenter.Global;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class LoginUtil {
	/**
	 * 设置最后一次登录的用户id
	 * @param context
	 * @param userId
	 */
	public static void setUserId(Context context, int userId) {
		SharedPreferences sp = context.getSharedPreferences(Global.PREFERENCE_CONFIG_FILE, Context.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putInt(Global.PREFERENCE_USER_ID, userId);
		edit.commit();
	}
	/**
	 * 得到当前登录的用户id
	 * @param context
	 * @return
	 */
	public static int getUserId(Context context) {
		SharedPreferences sp = context.getSharedPreferences(Global.PREFERENCE_CONFIG_FILE, Context.MODE_PRIVATE);
		return sp.getInt(Global.PREFERENCE_USER_ID, 0);
	}
	/**
	 * 设置最后一次登录的用户id
	 * @param context
	 * @param userId
	 */
	public static void setToken(Context context, String token) {
		SharedPreferences sp = context.getSharedPreferences(Global.PREFERENCE_CONFIG_FILE, Context.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putString(Global.PREFERENCE_TOKEN, token);
		edit.commit();
	}
	/**
	 * 得到当前登录的用户id
	 * @param context
	 * @return
	 */
	public static String getToken(Context context) {
		SharedPreferences sp = context.getSharedPreferences(Global.PREFERENCE_CONFIG_FILE, Context.MODE_PRIVATE);
		return sp.getString(Global.PREFERENCE_TOKEN, null);
	}
	/**
	 * 判断该用户id有没有在本机器上登录过
	 * @param context
	 * @param userId
	 * @return
	 */
	public static boolean isNewUser(Context context,int userId) {
		if (userId==0) {
			return false;
		}
		
		SharedPreferences sp = context.getSharedPreferences(Global.PREFERENCE_CONFIG_FILE, Context.MODE_PRIVATE);
		boolean b = sp.getBoolean(userId+"", true);
		
		if (b) {
			Editor edit = sp.edit();
			edit.putBoolean(""+userId, false);
			edit.commit();
		}
		
		return b;
	}
}
